﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.ExtensibleStorage;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.UI;
using YArchitech.LIB;
using YArchitech.Revit;

namespace YArchitech.MEP
{
	public class AuxiliaryFrameOperation
	{
		public void SetAuxiliaryFrameCurve(Autodesk.Revit.DB.Document doc, DetailCurve curve)
		{
			curve.LineStyle = doc.GetElement(DefinitionLineType.ReturnLineStyleId(doc, curve, "HY-AuxiliaryFrameLine"));
		}

		public Pipe SetAuxiliaryFramePipe(Autodesk.Revit.DB.Document doc, Pipe pipe, bool isAuxiliaryFrame, double Length, string StrIds)
		{
			return (Pipe)this.WriteAuxiliaryFrame(pipe, Convert.ToString(Length), StrIds);
		}

		public Pipe SetAuxiliaryFramePipeGroup(Autodesk.Revit.DB.Document doc, Pipe pipe, Group group)
		{
			Schema schema = this.CreateAuxiliaryFrameSchema();
			Entity entity = pipe.GetEntity(schema);
			if (entity == null || entity.Schema == null)
			{
				entity = new Entity(schema);
			}
			Field field = schema.GetField("GroupID");
			if (field != null && entity != null)
			{
				entity.Set<string>(field, group.Id.ToString());
				pipe.SetEntity(entity);
			}
			return pipe;
		}

		public void DeleteAllAuxiliaryFrame(Autodesk.Revit.DB.Document doc)
		{
			foreach (CurveElement curveElement in YJKElementFilter.GetElementByClass(typeof(CurveElement), doc).Cast<CurveElement>().ToList<CurveElement>())
			{
				if (curveElement.IsValidObject && curveElement is DetailCurve && curveElement.LineStyle.Name == "HY-AuxiliaryFrameLine" && curveElement.GroupId.IntegerValue > 0)
				{
					Element element = doc.GetElement(curveElement.GroupId);
					if (element != null && element.IsValidObject)
					{
						doc.DeleteElement(element);
					}
				}
			}
		}

		public void ShowAllAuxiliaryFrame(UIDocument uidoc)
		{
			string strIds = string.Empty;
			this.DeleteAllAuxiliaryFrame(uidoc.Document);
			foreach (Pipe pipe in YJKElementFilter.GetElementByClass(typeof(Pipe), uidoc.Document).Cast<Pipe>().ToList<Pipe>())
			{
				string text = this.ReadAuxiliaryFrameLength(pipe);
				if (!string.IsNullOrEmpty(text) && !(text == "0"))
				{
					if (!string.IsNullOrEmpty(text))
					{
						strIds = this.CreateAuxiliaryFrame(uidoc, ((LocationCurve)pipe.Location).Curve.GetEndPoint(0), Convert.ToDouble(text));
					}
					this.SetAuxiliaryFramePipe(uidoc.Document, pipe, true, Convert.ToDouble(text), strIds);
				}
			}
		}

		public string ReadAuxiliaryFrameGroupId(Element elem)
		{
			string result = string.Empty;
			Schema schema = this.CreateAuxiliaryFrameSchema();
			Entity entity = elem.GetEntity(schema);
			Field field = schema.GetField("GroupID");
			if (field != null && entity != null && entity.Schema != null)
			{
				result = entity.Get<string>(field);
			}
			return result;
		}

		public string ReadAuxiliaryFrameLength(Element elem)
		{
			string result = string.Empty;
			Schema schema = this.CreateAuxiliaryFrameSchema();
			Entity entity = elem.GetEntity(schema);
			Field field = schema.GetField("Length");
			if (field != null && entity != null && entity.Schema != null)
			{
				result = entity.Get<string>(field);
			}
			return result;
		}

		public string ReadAuxiliaryFrameStrIds(Element elem)
		{
			string result = string.Empty;
			Schema schema = this.CreateAuxiliaryFrameSchema();
			Entity entity = elem.GetEntity(schema);
			Field field = schema.GetField("StrIds");
			if (field != null && entity != null && entity.Schema != null)
			{
				result = entity.Get<string>(field);
			}
			return result;
		}

		private string CreateAuxiliaryFrame(UIDocument uidoc, XYZ ptCur, double length)
		{
			ElementSet elementSet = new ElementSet();
			string empty = string.Empty;
			if (length == 0.0)
			{
				return empty;
			}
			XYZ xyz = ptCur - new XYZ(NumbericTools.mmToFeet(length) / 2.0, -NumbericTools.mmToFeet(length) / 2.0, 0.0);
			XYZ xyz2 = ptCur - new XYZ(NumbericTools.mmToFeet(length) / 2.0, NumbericTools.mmToFeet(length) / 2.0, 0.0);
			XYZ xyz3 = ptCur - new XYZ(-NumbericTools.mmToFeet(length) / 2.0, -NumbericTools.mmToFeet(length) / 2.0, 0.0);
			XYZ xyz4 = ptCur - new XYZ(-NumbericTools.mmToFeet(length) / 2.0, NumbericTools.mmToFeet(length) / 2.0, 0.0);
			foreach (Curve curve in new List<Curve>
			{
				Line.CreateBound(xyz, xyz2),
				Line.CreateBound(xyz2, xyz4),
				Line.CreateBound(xyz4, xyz3),
				Line.CreateBound(xyz3, xyz)
			})
			{
				DetailCurve detailCurve = uidoc.Document.Create.NewDetailCurve(uidoc.ActiveView, curve);
				this.SetAuxiliaryFrameCurve(uidoc.Document, detailCurve);
				elementSet.Insert(detailCurve);
			}
			uidoc.Document.Regenerate();
			return uidoc.Document.CreatYJKGroup(elementSet).Id.ToString();
		}

		private Schema CreateAuxiliaryFrameSchema()
		{
			Schema schema = Schema.Lookup(AuxiliaryFrameOperation.AuxiliaryFrameGUID);
			if (schema != null)
			{
				return schema;
			}
			SchemaBuilder schemaBuilder = new SchemaBuilder(AuxiliaryFrameOperation.AuxiliaryFrameGUID);
			schemaBuilder.SetSchemaName("AuxiliaryFrameExtension");
			schemaBuilder.AddSimpleField("Length", typeof(string));
			schemaBuilder.AddSimpleField("StrIds", typeof(string));
			schemaBuilder.AddSimpleField("GroupID", typeof(string));
			return schemaBuilder.Finish();
		}

		private Element WriteAuxiliaryFrame(Element elem, string Length, string StrIds)
		{
			Schema schema = this.CreateAuxiliaryFrameSchema();
			Entity entity = elem.GetEntity(schema);
			if (entity == null || entity.Schema == null)
			{
				entity = new Entity(schema);
			}
			Field field = schema.GetField("Length");
			if (field != null && entity != null)
			{
				entity.Set<string>(field, Length);
				elem.SetEntity(entity);
			}
			Field field2 = schema.GetField("StrIds");
			if (field2 != null && entity != null)
			{
				entity.Set<string>(field2, StrIds);
				elem.SetEntity(entity);
			}
			return elem;
		}

		public static readonly Guid AuxiliaryFrameGUID = new Guid("{8244A72D-313D-490B-850F-49C09C0DFB21}");
	}
}
